<template>
  <div class="home-container">
    <img :src="importUrl('images/bg.png')" class="bg" />
    <div class="main-view">
      <div class="section">
        <div class="head">基本信息</div>
        <div class="cell">
          <div><span>*</span>用户姓名</div>
          <van-field
            v-model="form.userName"
            placeholder="请输入姓名"
            input-align="right"
          />
        </div>
        <div class="cell">
          <div>手机号码</div>
          <van-field
            v-model="form.phone"
            placeholder="请输入手机号"
            input-align="right"
          />
        </div>
        <div class="cell">
          <div>输入车牌号</div>
          <van-field
            v-model="form.plate"
            placeholder="请输入车牌号"
            input-align="right"
          />
        </div>
      </div>
      <div class="section">
        <div class="head">预约信息</div>
        <div class="cell" @click="show = true">
          <div>访问时间</div>
          <div
            class="text"
            :style="{ '--color': form.date == '' ? '#c8c9cc' : '#000' }"
          >
            {{ form.date == "" ? "请选择访问时间" : form.date }}
          </div>
        </div>
      </div>
    </div>

    <div class="foot">
      <div class="btn" @click="onSubmit">提交</div>
    </div>

    <van-calendar v-model:show="show" type="range" @confirm="onConfirm" />
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import { importUrl, getRouteParam } from "@/utils/utils.js";
import { showToast, showLoadingToast, closeToast } from "vant";
import dayjs from "dayjs";
import { addVehicleBookingForStaffByQR } from "@/service/interface/index.js";
import { sessionStorageFn } from "@/utils/storage.js";

const form = ref({
  userName: "",
  phone: "",
  plate: "",
  date: "",
  letInEndTime: "",
  letInStartTime: "",
});

onMounted(() => {
  //修改提交内容
  const isCustomer = getRouteParam("isCustomer") || false;
  document.title = !isCustomer ? "员工预约" : "访客预约";
  sessionStorageFn.set("isCustomer", isCustomer);
})

const show = ref(false);

// 时间选择
const onConfirm = (values) => {
  const [start, end] = values;
  show.value = false;
  form.value.date = `${dayjs(start).format("YYYY-MM-DD HH:mm:ss")} - ${dayjs(
    end
  ).format("YYYY-MM-DD HH:mm:ss")}`;
};

// 提交按钮
const onSubmit = () => {
  if (form.value.userName == "") {
    showToast("请输入访客姓名");
    return;
  }

  const arr = form.value.date.split(" - ");
  form.value.letInStartTime = arr[0];
  form.value.letInEndTime = arr[1];
  showLoadingToast({
    message: "加载中...",
    forbidClick: true,
  });

  const isCustomer = sessionStorageFn.get("isCustomer") || false;
  addVehicleBookingForStaffByQR(isCustomer,form.value)
    .then((data) => {
      closeToast();
      if (data) {
        showToast("提交成功");
        form.value = {
          userName: "",
          phone: "",
          plate: "",
          date: "",
          letInEndTime: "",
          letInStartTime: "",
        };
      } else {
        showToast("提交失败");
      }
    })
    .catch((err) => {
      closeToast();
      showToast(err);
    });
};
</script>

<style lang="less" scoped>
.home-container {
  width: 100vw;
  height: 100vh;
  background: #f6f6f6;
  position: relative;
  .bg {
    position: absolute;
    top: 0px;
    left: 0px;
    width: 100%;
    object-fit: contain;
    // height: 1016px;
  }
  .main-view {
    position: absolute;
    left: 30px;
    top: 272px;
    width: calc(100% - 60px);
    height: 690px;
    background: #ffffff;
    border-radius: 12px;
    display: flex;
    flex-direction: column;
    .section {
      padding: 0px 30px;
      box-sizing: border-box;
      .head {
        width: 100%;
        height: 108px;
        font-family: PingFang SC, PingFang SC;
        font-weight: 500;
        font-size: 32px;
        line-height: 108px;
        color: #181818;
      }
      .cell {
        width: 100%;
        height: 108px;
        font-family: PingFang SC, PingFang SC;
        font-weight: 400;
        font-size: 32px;
        line-height: 108px;
        color: #2c313c;
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding-right: 20px;
        box-sizing: border-box;
        .text {
          width: 60%;
          font-family: PingFang SC, PingFang SC;
          font-weight: 400;
          font-size: 32px;
          color: var(--color);
          text-align: right;
          line-height: 40px;
        }
        span {
          font-family: PingFang SC, PingFang SC;
          font-weight: 400;
          font-size: 32px;
          color: #ff5959;
        }
        :deep(.van-field) {
          width: 50%;
          padding: 0px;
          font-size: 32px;
        }
      }
    }
  }
  .foot {
    position: absolute;
    left: 0;
    bottom: 0px;
    width: 100%;
    height: 116px;
    background: #ffffff;
    box-shadow: 0px -2 0px 0px rgba(0, 0, 0, 0.06);
    border-radius: 0px 0px 0px 0px;
    .btn {
      margin: 18px auto;
      width: 686px;
      height: 80px;
      background: #2b72ed;
      border-radius: 16px 16px 16px 16px;
      font-family: PingFang SC, PingFang SC;
      font-weight: 400;
      font-size: 32px;
      color: #ffffff;
      line-height: 80px;
      text-align: center;
    }
  }
}
</style>
